﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using IDAL.Cms;
using IDAL;
using Model.Cms;
using Model.Config;
using Model.Base;
namespace SQLServerDAL.Cms
{
    /// <summary>
    /// 数据层，内容管理，节点管理
    /// </summary>
    public class NodeDAL : DalBase, INode
    {
        public NodeDAL() 
        {
            Schema = "cms.";
            TableName = Schema + "Node";
            ViewTableName = Schema + "V_Node";
            FieldsList = new string[] { 
                "NodeID, NodeName, SeoKeyword, HeadScript, BodyContent, AddTime, NodeChannelID, NodeChannelName, NodeChannelLabel"
                ,"NodeID, NodeName, SeoKeyword, HeadScript, AddTime, NodeChannelID, NodeChannelName, NodeChannelLabel"
            };
        }

        #region INode 成员
        /// <summary>
        /// 新增节点
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int InsertNodel(NodeInfo info)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@NodeChannel",SqlDbType.Int)
                ,new SqlParameter("@NodeName",SqlDbType.NVarChar,40)
                ,new SqlParameter("@SeoKeyword",SqlDbType.NVarChar,100)
                ,new SqlParameter("@HeadScript",SqlDbType.NVarChar,400)
                ,new SqlParameter("@BodyContent",SqlDbType.NText)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = info.NodeChannel.TermID;
            parm[i++].Value = info.NodeName;
            parm[i++].Value = info.SeoKeyword;
            parm[i++].Value = info.HeadScript;
            parm[i++].Value = info.BodyContent;
            parm[i++].Direction = ParameterDirection.Output;
            return SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure
                , Schema + "AddNode", parm);
        }

        /// <summary>
        /// 修改节点
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool UpdateNode(NodeInfo info)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@NodeChannel",SqlDbType.Int)
                ,new SqlParameter("@NodeName",SqlDbType.NVarChar,40)
                ,new SqlParameter("@SeoKeyword",SqlDbType.NVarChar,100)
                ,new SqlParameter("@HeadScript",SqlDbType.NVarChar,400)
                ,new SqlParameter("@BodyContent",SqlDbType.NText)
                ,new SqlParameter("@NodeID",SqlDbType.Int)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = info.NodeChannel.TermID;
            parm[i++].Value = info.NodeName;
            parm[i++].Value = info.SeoKeyword;
            parm[i++].Value = info.HeadScript;
            parm[i++].Value = info.BodyContent;
            parm[i++].Value = info.NodeID;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure
                , Schema + "UpdateNode", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="nodeID"></param>
        /// <param name="nodeList"></param>
        /// <param name="staffID"></param>
        /// <returns></returns>
        public bool DeleteNodel(int nodeID, string nodeList, int staffID)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@NodeID",SqlDbType.Int)
                ,new SqlParameter("@NodeList",SqlDbType.VarChar,100)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
            };
            int i = 0;
            parm[i++].Value = nodeID;
            parm[i++].Value = nodeList;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "DeleteNode", parm);
            return parm[parm.Length - 1].Value.ToString().Equals("80");
        }

        public NodeInfo GetNodeByID(int id)
        {
            Sql = string.Format("SELECT TOP 1 {0} FROM {1} WHERE NodeID = {2}", FieldsList[0], ViewTableName, id);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, Sql);
            NodeInfo info = new NodeInfo();
            if (dr != null) 
            {
                if (dr.Read()) 
                {
                    int i = 0;
                    info.NodeID = dr.GetInt32(i++);
                    info.NodeName = dr.GetString(i++);
                    info.SeoKeyword = dr.GetString(i++);
                    info.HeadScript = dr.GetString(i++);
                    info.BodyContent = dr.GetString(i++);
                    info.AddTime = dr.GetDateTime(i++);
                    info.NodeChannel = new TermInfo();
                    info.NodeChannel.TermID = dr.GetInt32(i++);
                    info.NodeChannel.TermName = dr.GetString(i++);
                    info.NodeChannel.TermLabel = dr.GetString(i++);
                }
                dr.Close();
            }
            return info;
        }

        public IList<NodeInfo> ListNode(NodeCondition condition)
        {
            SqlBuilder = new StringBuilder("SELECT ");
            if (condition.Top > 0)
                SqlBuilder.AppendFormat(" TOP ({0}) ", condition.Top);
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1 ", FieldsList[1], ViewTableName);
            if (condition.NodeChannelID > 0)
                SqlBuilder.AppendFormat(" AND NodeChannelID = {0}", condition.NodeChannelID);
            if (!string.IsNullOrEmpty(condition.NodeChannelLabel))
                SqlBuilder.AppendFormat(" AND NodeChannelLabel = '{0}'", condition.NodeChannelLabel);
            if (!string.IsNullOrEmpty(condition.OrderBy))
                SqlBuilder.AppendFormat(" ORDER BY {0}", condition.OrderBy);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            List<NodeInfo> list = new List<NodeInfo>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    NodeInfo info = new NodeInfo();
                    int i = 0;
                    info.NodeID = dr.GetInt32(i++);
                    info.NodeName = dr.GetString(i++);
                    info.SeoKeyword = dr.GetString(i++);
                    info.HeadScript = dr.GetString(i++);
                    info.AddTime = dr.GetDateTime(i++);
                    info.NodeChannel = new TermInfo();
                    info.NodeChannel.TermID = dr.GetInt32(i++);
                    info.NodeChannel.TermName = dr.GetString(i++);
                    info.NodeChannel.TermLabel = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        public IList<NodeInfo> ListNode(NodeCondition condition, out int recordCount)
        {
            Wheres = new StringBuilder(" 1 = 1 ");
            if (condition.NodeChannelID > 0)
                Wheres.AppendFormat(" AND NodeChannelID = {0}", condition.NodeChannelID);
            if (!string.IsNullOrEmpty(condition.NodeChannelLabel))
                Wheres.AppendFormat(" AND NodeChannelLabel = '{0}'", condition.NodeChannelLabel);
            recordCount = base.PageListCounter(ViewTableName, Wheres.ToString());
            SqlDataReader dr = base.PageListRecord(FieldsList[1], ViewTableName, Wheres.ToString()
                , condition.OrderBy, condition.PageIndex, condition.PageSize);
            List<NodeInfo> list = new List<NodeInfo>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    NodeInfo info = new NodeInfo();
                    int i = 0;
                    info.NodeID = dr.GetInt32(i++);
                    info.NodeName = dr.GetString(i++);
                    info.SeoKeyword = dr.GetString(i++);
                    info.HeadScript = dr.GetString(i++);
                    info.AddTime = dr.GetDateTime(i++);
                    info.NodeChannel = new TermInfo();
                    info.NodeChannel.TermID = dr.GetInt32(i++);
                    info.NodeChannel.TermName = dr.GetString(i++);
                    info.NodeChannel.TermLabel = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        #endregion
    }
}
